package com.hongguan.oms.web.supervise;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.Uploader;
import com.hongguan.oms.domain.UserInfo;
import com.hongguan.oms.domain.supervise.SuperviseAuditModel;
import com.hongguan.oms.domain.supervise.SuperviseInfoModel;
import com.hongguan.oms.service.MsgAgentService;
import com.hongguan.oms.service.UserInfoService;
import com.hongguan.oms.service.supervise.SuperviseService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;

/**
 * �������
 * 
 * @author ��ΰ
 * @version createTime:2010-2-25 ����04:35:09 summary
 */
public abstract class AbstractSuperviseHandler extends BaseHandler {

	protected SuperviseService service;
	protected UserInfoService userService;
	protected Uploader uploader;
	protected MsgAgentService msgService;
	
	protected void initParam(UserInfo user, Map<String, Object> param, String[] states) {
		param.put("filialeId", user.getFilialeId());
		if (states != null) {
			param.put("superviseState", states);
		} else {
			String state = (String) param.get("superviseState");
			if (state != null && state.length() > 0)
				param.put("superviseState", new String[] { state });
		}
	}

	// public ModelAndView addSupervise(HttpServletRequest request,
	// HttpServletResponse respnose, SuperviseInfoModel model)
	// throws Exception {
	// UserInfo user = Util.getUserInfoFromSession(request);
	// if (user == null)
	// return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
	// model.setCreateId(user.getId());
	// model.setSuperviseType(this.getType());
	// model.setSuperviseState("2");
	// model.setCloseFlag("0");
	// model.setImportantFlag("0");
	// service.addSupervise(model);
	// ModelAndView mv = this.getSuccessModelView();
	// return mv;
	// }

	public ModelAndView fulfill(HttpServletRequest request, HttpServletResponse respnose, SuperviseInfoModel model)
			throws Exception {
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		model.setSuperviseState("5");
		service.fulfill(model);
		ModelAndView mv = this.getSuccessModelView();
		return mv;
	}

	public ModelAndView feedback(HttpServletRequest request, HttpServletResponse respnose, SuperviseInfoModel model)
			throws Exception {
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		model.setSuperviseState("4");
		service.feedback(model);
		ModelAndView mv = this.getSuccessModelView();
		return mv;
	}

	public ModelAndView listFeedback(HttpServletRequest request, HttpServletResponse respnose) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("superviseId", request.getParameter("superviseId"));
		param.put("superviseType", this.getType());
		ModelAndView mv = this.getSuccessModelView();
		mv.addObject("result", service.listFeedback(param));
		return mv;
	}

	public ModelAndView transImportant(HttpServletRequest request, HttpServletResponse respnose) throws Exception {
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		String sid = request.getParameter("ids");
		String[] ids = sid.split(",");
		Map<String, Object> param = new HashMap<String, Object>(2);
		param.put("importantFlag", "1");
		param.put("superviseIds", ids);
		service.transImportant(param);
		ModelAndView mv = this.getSuccessModelView();
		return mv;
	}

	public ModelAndView transClose(HttpServletRequest request, HttpServletResponse respnose) throws Exception {
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		String sid = request.getParameter("ids");
		String[] ids = sid.split(",");
		Map<String, Object> param = new HashMap<String, Object>(2);
		param.put("closeFlag", "1");
		param.put("superviseIds", ids);
		service.transClose(param);
		ModelAndView mv = this.getSuccessModelView();
		return mv;
	}

	protected abstract String getType();

	protected ModelAndView searchSupervise(HttpServletRequest request, HttpServletResponse respnose, String[] states)
			throws Exception {
		return searchSupervise(request, respnose, null, states);
	}

	protected ModelAndView searchSupervise2(HttpServletRequest request, HttpServletResponse respnose, String[] states)
			throws Exception {
		return searchSupervise(request, respnose, "managerId", states);
	}

	protected ModelAndView searchSupervise3(HttpServletRequest request, HttpServletResponse respnose, String[] states)
			throws Exception {
		return searchSupervise(request, respnose, "createId", states);
	}
	protected ModelAndView searchSupervise4(HttpServletRequest request, HttpServletResponse respnose, String[] states)
			throws Exception {
		return searchSupervise(request, respnose, null, states);
	}
	private ModelAndView searchSupervise(HttpServletRequest request, HttpServletResponse respnose, String key,
			String[] states) throws Exception {
		// ����û���Ϣ
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		Map<String, Object> param = paramQuery(request);
		param.put("superviseType", this.getType());
		initParam(user, param, states);
		if (key != null)
			param.put(key, user.getId());
		//param.put("filialeId", user.getFilialeId());
		ModelAndView mv = this.getSuccessModelView();
		mv.addObject("result", service.searchSupervise(param));
		mv.addObject("total", service.totalSupervise(param)); 
		return mv;
	}

	protected ModelAndView updateState(HttpServletRequest request, HttpServletResponse respnose, String state)
			throws Exception {
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		String sid = request.getParameter("ids");
		String[] ids = sid.split(",");
		String remind=request.getParameter("remind");
		Map<String, Object> param = new HashMap<String, Object>(2);
		param.put("superviseState", state);
		param.put("superviseIds", ids);
		service.submitSupervise(param);
		if(state!=null&&"1".equals(state)
				&&remind!=null&&remind.length()>0){
			for(int i=0;i<ids.length;i++){
				if(ids[i]==null||ids[i].length()==0)
					continue;
				param.put("superviseId",ids[i]);
				SuperviseInfoModel model=service.loadSupervise(param);
				model.setRemind(remind);
				model.setSubmitTime(new Date());
				UserInfo auditor=userService.getUserById(model.getManagerId());
				this.remind(auditor, model);
			}
		}		
		ModelAndView mv = this.getSuccessModelView();
		return mv;
	}
	
	public ModelAndView searchUpLoadFiles(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		Map<String,Object> param = new HashMap<String ,Object>();
		param.put("buis_Id", Util.toNull(request.getParameter("superviseId")));
		int beginRow = 0;
		int endRow = 1000;
		param.put("beginRow", beginRow);
		param.put("endRow", endRow);
		mv = this.getSuccessModelView();
		mv.addObject("result", uploader.searchUpLoadFile(param));
		return mv;
	}
	public String downloadFile(HttpServletRequest request,HttpServletResponse response) throws Exception{
		return uploader.downloadFile(request, response);
    }
	protected void remind(UserInfo user, SuperviseInfoModel model) {
		Set<String> numSet = new HashSet<String>();
		Set<String> mailSet = new HashSet<String>();
		if(user.getPhone()!=null){
			numSet.add(user.getPhone());
		}
		if(user.getEmail()!=null){
			mailSet.add(user.getEmail());
		}
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		if ("1".equals(model.getRemind())||"3".equals(model.getRemind())) {
			StringBuffer sb = new StringBuffer();
			sb.append("�������ѣ�");
			sb.append(model.getSuperviseTitle());
			sb.append("���쵼��ʾ���");
			sb.append(model.getNotion());
			sb.append("������ʱ��: ");
			sb.append(format.format(model.getSubmitTime()));
			sb.append(" �뼰ʱ��¼OMSϵͳ���д���");
			msgService.sendSMS(numSet, sb.toString(), new Date().toString());
		}
		if ("2".equals(model.getRemind())||"3".equals(model.getRemind())) {
			// �����ʼ�
			StringBuffer sb = new StringBuffer();
			sb.append("�������ѣ�");
			sb.append(model.getSuperviseTitle());
			sb.append("���쵼��ʾ���");
			sb.append(model.getNotion());
			sb.append("������ʱ��: ");
			sb.append(format.format(model.getSubmitTime()));
			sb.append(" �뼰ʱ��¼OMSϵͳ���д���");
			msgService.sendMail(mailSet, model.getSuperviseTitle(), sb.toString(), new Date().toString());
		}
	}
	public void setUserService(UserInfoService userService) {
		this.userService = userService;
	}

	public void setMsgService(MsgAgentService msgService) {
		this.msgService = msgService;
	}

	public void setUploader(Uploader uploader) {
		this.uploader = uploader;
	}
}
